package com.gourd.common.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.gourd.common.annotation.Log;
import com.gourd.common.rbac.dto.RbacUserDTO;
import com.gourd.common.rbac.dto.RbacUserSearchDTO;
import com.gourd.common.rbac.entity.RbacUser;
import com.gourd.common.rbac.service.RbacUserService;
import com.gourd.common.rbac.vo.JwtUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author gourd
 */
@RestController
@RequestMapping("/user")
@Api(tags = "user", description = "用户控制器")
public class UserController{

    @Autowired
    private RbacUserService userService;


    @GetMapping("/current")
    @ApiOperation(value = "获取当前用户")
    @PreAuthorize("hasAuthority('READ')")
    @ApiImplicitParams({@ApiImplicitParam(name = "jwt-token", value = "jwt-token", required = true, dataType = "string", paramType = "header")})
    public JwtUser getCurrent() {
        return userService.getCurrent();
    }

    @GetMapping("/all")
    @ApiOperation(value = "获取所有的用户")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @ApiImplicitParams({@ApiImplicitParam(name = "jwt-token", value = "jwt-token", required = true, dataType = "string", paramType = "header")})
    public List<RbacUser> findAll() {
        return userService.findAll();
    }

    @GetMapping("/page")
    @ApiOperation(value = "根据条件获取用户分页")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @ApiImplicitParams({@ApiImplicitParam(name = "jwt-token", value = "jwt-token", required = true, dataType = "string", paramType = "header")})
    public Page<RbacUser> find(RbacUserSearchDTO rbacUserDTO) {
        PageRequest pageRequest = new PageRequest(rbacUserDTO.getPageNo() - 1, rbacUserDTO.getPageSize());
        Page<RbacUser> rbacUserPage = userService.find(rbacUserDTO, pageRequest);
        return rbacUserPage;

    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取用户")
    @PreAuthorize("hasAuthority('READ')")
    @ApiImplicitParams({@ApiImplicitParam(name = "jwt-token", value = "jwt-token", required = true, dataType = "string", paramType = "header")})
    public RbacUser getDetail(@PathVariable Long id) {
        return userService.getById(id);
    }

    @GetMapping("/account-by")
    @ApiOperation(value = "根据账号获取用户")
    @PreAuthorize("hasAuthority('READ')")
    @ApiImplicitParams({@ApiImplicitParam(name = "jwt-token", value = "jwt-token", required = true, dataType = "string", paramType = "header")})
    public RbacUser getByAccount(@RequestParam String account) {
        return userService.getByAccount(account);
    }


    @PostMapping("/add")
    @ApiOperation(value = "创建用户")
    @PreAuthorize("hasAuthority('WRITE')")
    @Log("创建用户")
    @ApiImplicitParams({@ApiImplicitParam(name = "jwt-token", value = "jwt-token", required = true, dataType = "string", paramType = "header")})
    public RbacUser getByAccount(@RequestBody @Validated RbacUserDTO rbacUserDTO) {
        return userService.register(rbacUserDTO);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除用户")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @Log("删除用户")
    @ApiImplicitParams({@ApiImplicitParam(name = "jwt-token", value = "jwt-token", required = true, dataType = "string", paramType = "header")})
    public void delete(@PathVariable Long id) {
        userService.delete(id);
    }
}
